﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Entities;
using BAL.Components;
using System.Data;
using DigitalAnts.MasterPage;
using System.Drawing;
using DigitalAnts.App_Code.BAL.Components;

namespace DigitalAnts.User
{
    public partial class EditProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ((UserLayout)Page.Master).UserInfoHyperLink = false;
                ((UserLayout)Page.Master).UserInfoHyperLinkColor = Color.Red;
                ((UserLayout)Page.Master).UnabelUserInfoHyplerLinkUnderline = false;
                doLoadUserProfile();
            }
        }

        public void doLoadUserProfile()
        {
            clsUser obj = new clsUser();
            obj.UserID = Convert.ToInt32(Session["UserID"]) ;
            obj = clsUserBAL.doGetUserProfile(obj);
            this.txtFirstName.Text = obj.FirstName;
            this.txtLastName.Text = obj.LastName;
            this.txtAdress.Text = obj.Address;
            this.txtBirthDay.Text = String.Format("{0:dd/MM/yyyy}", obj.BirthDay);
            this.txtEmail.Text = obj.Email;
            this.txtPhoneNumber.Text = "0" + obj.PhoneNumber;
            this.txtHomePhone.Text = "0" + obj.HomePhone;
            this.txtIndentityCard.Text = obj.IdentityCard;
            if (!IsPostBack)
            {
                CountryList();
                clsCountry co = new clsCountry();
                co.CountryID = obj.CountryID.ToString();
                CityList(co);
                this.ddtCountry.SelectedValue = obj.CountryID.ToString();
                this.ddlCity.SelectedValue = obj.CityID.ToString();
            }
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
                clsUser obj = new clsUser();
                obj.UserID = Convert.ToInt32(Session["UserID"]);
                obj.FirstName = this.txtFirstName.Text;
                obj.LastName = this.txtLastName.Text;
                obj.BirthDay = DateTime.Parse(this.txtBirthDay.Text);
                obj.IdentityCard = this.txtIndentityCard.Text;
                obj.HomePhone = this.txtHomePhone.Text;
                obj.PhoneNumber = this.txtPhoneNumber.Text;
                obj.CityID = Convert.ToInt32(this.ddlCity.SelectedValue);
                obj.Address = this.txtAdress.Text;
                if (clsUserBAL.doUpdateUserProfile(obj) == true)
                {

                }
        }

        public void CountryList()
        {
            DataTable countrydt = clsCountryBAL.doGetCountry();
            this.ddtCountry.DataSource = countrydt;
            this.ddtCountry.DataTextField = countrydt.Columns["Country"].ColumnName.ToString();
            this.ddtCountry.DataValueField = countrydt.Columns["CountryID"].ColumnName.ToString();
            this.ddtCountry.DataBind();
        }

        public void CityList(clsCountry obj)
        {
            DataTable citydt = clsCountryBAL.doGetCity(obj);

            this.ddlCity.Items.Clear();
            if (citydt.Rows.Count > 0)
            {
                this.ddlCity.DataSource = citydt;
                this.ddlCity.DataTextField = citydt.Columns["CityName"].ColumnName.ToString();
                this.ddlCity.DataValueField = citydt.Columns["CityID"].ColumnName.ToString();
                this.ddlCity.DataBind();
            }
            else
            {
                this.ddlCity.AppendDataBoundItems = true;
                this.ddlCity.Items.Insert(0, new ListItem(" - - - Hãy chọn quốc gia khác - - - ", String.Empty));
                this.ddlCity.Enabled = false;
            }
        }



        protected void ddtCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            clsCountry obj = new clsCountry();
            obj.CountryID = this.ddtCountry.SelectedValue;
            this.ddlCity.Enabled = true;
            CityList(obj);
        }
    }
}